package com.yuuzy.dao;


import com.alibaba.druid.util.StringUtils;
import com.yuuzy.model.Client;

import java.util.List;

public class ClientDAO extends BasicDAO<Client> {

    /**
     * 查询客户信息
     *
     * @param client 客户实体（传入未初始化的client对象可以查询全部客户）
     * @return 查询到的客户集
     */

    public List<Client> queryClient(Client client) {
        StringBuilder sb = new StringBuilder("select * from client");//查询姓名为？的客户在client表中的信息
        if (!StringUtils.isEmpty(client.getClient_name())) {
            //根据用户姓名查询
            sb.append(" and client_name like '%").append(client.getClient_name()).append("%'");//支持模糊查询
        } else if (client.getClient_id() != null && !StringUtils.isEmpty(client.getClient_password())) {
            //根据用户id和密码查询
            sb.append(" and client_id = ").append(client.getClient_id());
            sb.append(" and client_password = '").append(client.getClient_password()).append("'");
        }
        String sql = sb.toString().replaceFirst("and", "where");

        return queryMulti(sql, Client.class);
    }

    /**
     * 增加客户信息
     *
     * @param client 要增加的客户实体
     * @return 受影响的行数（大于0为增加成功）
     */
    public int insertClient(Client client) {
        String sql = "INSERT INTO finance.client(client_name,client_telephone,client_idnumber,client_email,client_password,client_balance) VALUES (?,?,?,?,?,?)";//添加客户信息
        return update(sql, client.getClient_name(), client.getClient_telephone(), client.getClient_idnumber(), client.getClient_email(), client.getClient_password(), client.getClient_balance());
    }

    /**
     * 删除客户信息
     *
     * @param client 要删除的客户实体
     * @return 受影响的行数（大于0为删除成功）
     */
    public int deleteClient(Client client) {
        StringBuilder sb = new StringBuilder("DELETE FROM client");//删除id和姓名相匹配的客户(两者至少有一个）
        if (!StringUtils.isEmpty(client.getClient_name())) {
            sb.append(" and client_name = '").append(client.getClient_name()).append("'");
        }
        if (client.getClient_id() != 0) {
            sb.append(" and client_id = '").append(client.getClient_id()).append("'");

        }
        String sql = sb.toString().replaceFirst("and", "where");
        return update(sql);
    }

    /**
     * 更新客户信息
     *
     * @param client 更新后的客户实体
     * @return 受影响的行数
     */
    public int updateClient(Client client) {
        //id不能更改
        StringBuilder sb = new StringBuilder("UPDATE client SET");
        if (!StringUtils.isEmpty(client.getClient_name())) {
            //如果姓名字段存在
            sb.append(" ,client_name = '").append(client.getClient_name()).append("'");
        }
        if (!StringUtils.isEmpty(client.getClient_telephone())) {
            //如果电话字段存在
            sb.append(" ,client_telephone = '").append(client.getClient_telephone()).append("'");
        }
        if (!StringUtils.isEmpty(client.getClient_idnumber())) {
            //如果身份证号字段存在
            sb.append(" ,client_idnumber = '").append(client.getClient_idnumber()).append("'");
        }
        if (client.getClient_email() != null) {
            //如果电子邮箱字段存在
            sb.append(" ,client_email= '").append(client.getClient_email()).append("'");
        }
        if (client.getClient_password() != null) {
            //如果密码字段存在
            sb.append(" ,client_password = '").append(client.getClient_password()).append("'");
        }
        if (client.getClient_balance() != null) {
            //如果余额字段存在
            sb.append(" ,client_balance = ").append(client.getClient_balance());
        }
        String sql = sb.append(" WHERE client_id = ").append(client.getClient_id()).toString().replaceFirst(",", "");
        return update(sql);
    }
}